package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Types;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Connectivity.ConnectionManager;
import Connectivity.Logger;

public class ItemEntry extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ItemEntry() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException 
			{
			Connection con=null;
		response.setContentType("text/html");
		//groupsel=2&itemcode=D001&itemname=DELL4300&itemdesc=Light+weight+model
		//&companyname=DELL&submit=submit&itemgroup_id=0
		String groupsel = request.getParameter("groupsel")!=null?request.getParameter("groupsel"):"";
		 String itemcode = request.getParameter("itemcode")!=null?request.getParameter("itemcode"):"";
		 String itemname = request.getParameter("itemname")!=null?request.getParameter("itemname"):"";
		 String subgroup = request.getParameter("subgroup")!=null?request.getParameter("subgroup"):"1";
		 String itemdesc = request.getParameter("itemdesc")!=null?request.getParameter("itemdesc"):""+itemname;
		 String companyname = request.getParameter("companyname")!=null?request.getParameter("companyname"):"-";
		 String submit = request.getParameter("submit")!=null?request.getParameter("submit"):"";
		
		 HttpSession session = request.getSession(true);
		 try
		    {
		   	PreparedStatement cstmt = null;
				int calldisplayrs = 0;
		 
				System.out.println("itemcode:=>"+itemcode);
				System.out.println("itemname:=>"+itemname);
				System.out.println("subgroup:=>"+subgroup);
				System.out.println("itemdesc:=>"+itemdesc);
				System.out.println("companyname:=>"+companyname);
				System.out.println("groupsel:=>"+groupsel);
				
				
				subgroup="1";
				
				//CallableStatement cs = null;
				 
				
			/*	cs = con.prepareCall("{? = call fun_initem(?,?,?,?,?,?)}");
				     cs.registerOutParameter(1,Types.INTEGER);
					 cs.setString(2,itemcode);
		 			 cs.setString(3,itemname);
		 			cs.setString(4,subgroup);
		 			cs.setString(5,itemdesc);
		 			cs.setString(6,companyname);
		 			cs.setString(7,groupsel);					 
					cs.execute();*/
				
				PreparedStatement cs=con.prepareStatement("INSERT INTO item_master(item_name, item_code, item_desc, uom,company_name,group_id,subgroup_id)values (?,?,?,1,?,?,1)");
				  cs.setString(1,itemname);
  
				  cs.setString(2,itemcode);
	 			cs.setString(3,itemdesc);
	 			cs.setString(4,companyname);
	 			cs.setInt(5,Integer.parseInt(groupsel));
	 				
                cs.execute();
					
					///int call_performed_id =(cs.getInt(1));
					System.out.println("ItemEntry:"+itemname);
					
					//calldisplayrs =Integer.parseInt(""+cstmt.getObject(1));
					
		 
		 	//if(calldisplayrs==1)
		 //	{
		 		response.sendRedirect("/Genius/form/masters/ItemEntry.jsp?status=I");
		 	//}
		 	
		 	}
		 catch(Exception e)
		 {
			 e.printStackTrace();
			 }

			
			
			
			
			
			
			
			
		 
		/// System.out.println("I  am here so dont worry");
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
